// chanscan utilizes a ring to implement a 'wheel':
package main

import (
	"container/ring"
	"fmt"
	"math/rand"
	"time"
)

type mydat struct {
	d time.Time
	n int
}

const ringSize = 10

var values *ring.Ring

// regularAdd adds a value to the value ring at regular time
// intervals.
func regularAdd() {

	i := 10

	for {

		<-time.After(1000 * time.Millisecond)
		i++
		v := mydat{d: time.Now(), n: i}
		values.Value = v
		values = values.Next()

	}

}

// irregularScan processes the contents of the value ring at
// unpredictable times. Processing begins at the oldest value.
func irregularScan() {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))

	for {
		<-time.After(time.Duration(r.Intn(200)) * time.Millisecond)
		values.Do(func(v interface{}) {
			fmt.Print(v.(mydat).n, " ")
		})
		fmt.Println("")
	}

}

func main() {
	values = ring.New(ringSize)

	for i := 0; i < ringSize; i++ {
		values.Value = mydat{d: time.Now(), n: 10}
		values = values.Next()
	}

	go irregularScan()
	go regularAdd()

	time.Sleep(20 * time.Second)
}
